<?php

namespace controller;

/**
 * Description of Search
 *
 * @author Damian
 */
class Search extends Shop {

    public function index() {
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle", "Wyszukiwanie zaawansowane");      
        $manufacturers = \model\TblManufacturer::getModel()->getAllByShopID($this->shop->ShopID);
        $manufacturersData = new \model\DataTable($manufacturers);
        $manufacturersModelData = $manufacturersData->getModelData(true);        
        $categories = \model\TblCategory::getModel()->getAllByShopID($this->shop->ShopID);
        $categoriesData = new \model\DataTable($categories);
        $categoriesModelData = $categoriesData->getModelData(true);
        $view = $this->getActionView();
        $view->set("manufacturers", $manufacturersModelData);
        $view->set("categories", $categoriesModelData);
         $session = \library\Registry::get("session");
         $session->erase("from");
         $session->erase("to");
         $session->erase("name");
         $session->erase("man");
         $session->erase("cat");
        $this->render();
    }
    public function results(){
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle", "Wyniki wyszukiwania");
        $view = $this->getActionView();
        $page = (\library\RequestMethods::get("Page")) ? \library\RequestMethods::get("Page") : 1;
        $session = \library\Registry::get("session");
        $productsCriteria=  \model\TblProduct::getModel()->createCriteria($this->shop->ShopID);
        $productsCriteria->where(\model\TblProduct::ACTIVE, "=", 1,  \model\TblProduct::getModel()->getTable()," and ");               
        //istnieje kryterium kategorii
        if (\library\RequestMethods::get("cat") && is_numeric(\library\RequestMethods::get("cat")) || $session->get("cat")){
            $cat = (\library\RequestMethods::get("cat"))?\library\RequestMethods::get("cat"):  $session->get("cat");
            $catCriteria = new \model\Criteria();
            $catCriteria->from(\model\TblCategory::getModel()->getTable())
                    ->where(\model\TblCategory::SHOP_ID, "=", $this->shop->ShopID)
                    ->where(\model\TblCategory::ID, "=", $cat,false, " and ");
            $category = \model\TblCategory::getModel()->first($catCriteria);
            if ($category){
                $session->set("cat",$cat);
                $IDs = $category->getAllIds();
                $productsCriteria->in(\model\TblProduct::CATEGORY_ID, $IDs,  \model\TblCategory::getModel()->getTable()," and ");
            }                  
        }
       
        //kryterium producenta
       if (\library\RequestMethods::get("man") && is_numeric(\library\RequestMethods::get("man")) || $session->get("man")){
           $man = (\library\RequestMethods::get("man"))? \library\RequestMethods::get("man"): $session->get("man");
           $productsCriteria->where(\model\TblProduct::MANUFACTURER_ID, "=", $man,  \model\TblProduct::getModel()->getTable(), " and ");
           $session->set("man",$man);
       }
       //kryterium nazwy
       if (\library\RequestMethods::get("name") || $session->get("name")){
           $name = (\library\RequestMethods::get("name"))?\library\RequestMethods::get("name"): $session->get("name");
           $productsCriteria->where(\model\TblProduct::NAME, " like ", "%".$name."%",  \model\TblProduct::getModel()->getTable(), " and ");
           $session->name("name",$name);
       }
       //kryterium ceny
        if (\library\RequestMethods::get("from") && is_numeric(\library\RequestMethods::get("from")) || $session->get("from")){
            $from = (\library\RequestMethods::get("from"))? \library\RequestMethods::get("from"): $session->get("from");
            $productsCriteria->where(\model\TblProduct::GROSS_PRICE, ">", $from,  \model\TblProduct::getModel()->getTable(), " and ");
            $session->set("from",$from);
        }
        if (\library\RequestMethods::get("to") && is_numeric(\library\RequestMethods::get("to")) || $session->get("to")){
            $to = (\library\RequestMethods::get("to"))? \library\RequestMethods::get("to"): $session->get("to");
            $productsCriteria->where(\model\TblProduct::GROSS_PRICE, "<", $to,  \model\TblProduct::getModel()->getTable(), " and ");
            $session->set("to",$to);
        }
        $countCriteria = $productsCriteria;
        $all=$countCriteria->count();
        $productsCriteria->limit($this->perPage,$page);
        $Products = \model\TblProduct::getModel()->all($productsCriteria);
        $pagesCount = ceil($all / $this->perPage);
        $pages = new \view\Pagination($page, 1, $pagesCount, "./" . $this->shop->URL . "/" . "sm");
        if ($Products) {
            $view->set("productsPictures", $Products);
            $layoutView->set("pages",$pages);
        }
        $this->render();
    }
}
